<!--
 * @Date         : 2023-11-30 09:14:35
 * @Description  : 广告弹窗组件
-->
<template>
  <zm-dialog :show.sync="visible" :show-confirm-button="false" padding="0" background="transparent">
    <view v-if="list[index]" class="flex flex-1 items-center justify-center">
      <image :src="list[index].imageUrl" mode="widthFix" @click="handleClick" />
    </view>
    <view slot="outside" class="flex justify-center mt-24">
      <zm-icon name="close" color="#fff" size="74rpx" @click="handleClose"> </zm-icon>
    </view>
  </zm-dialog>
</template>

<script>
import mixin from "@/decoration/mixins"
import { mapGetters } from "vuex"
// 广告弹窗类型字典 1 首页 2 送呗 3 商城首页 4 个人中心 5 电影详情 6 商品详情
const AD_PAGE_TYPE = { index: "1", giftGiving: "2", mallIndex: "3", person: "4", orderFilmInfo: "5", orderGoodsInfo: "6" }
export default {
  name: "deco-ad-popup",
  mixins: [mixin],
  props: {
    // 是否自动打开弹窗
    auto: { type: Boolean, default: true },
    // 页面类型
    page: { type: String, default: "" }
  },
  data() {
    return {
      list: [],
      index: -1,
      loading: false,
      visible: false
    }
  },
  computed: {
    ...mapGetters(["launchId"])
  },
  mounted() {
    this.init()
    this.handleGetPopupList()
  },
  methods: {
    init() {
      if (this.mitt) {
        this.mitt.on("refresh", () => {
          this.handleGetPopupList()
        })
      }
    },
    // 点击
    handleClick() {
      this.useAdJump(this.list[this.index])
    },
    async handleGetPopupList() {
      if (this.loading || this.visible) return
      this.loading = true
      const lsAds = uni.$getCache("ad-popup-op") || []
      try {
        const list = await uni.$api.poster.getPopupList()
        this.list = list.filter((item) => {
          // 过滤类型不相等的
          if (item.showPage != AD_PAGE_TYPE[this.page]) return false
          // 过滤不展示的
          if (lsAds.length) {
            const find = lsAds.find((lsItem) => lsItem.adId === item.adId)
            if (find?.showRule == 1 && item.showRule == 1) return false
            if (find?.showRule == 2 && item.showRule == 2 && find?.launchId == this.launchId) return false
          }
          return true
        })
        if (this.list?.length && this.auto) {
          this.index = 0
          this.loading = false
          this.visible = true
        }
      } catch (error) {
        this.loading = false
      }
    },
    // 处理关闭广告
    async handleClose(type = "2") {
      // 操作类型 1 点击 2 关闭 3 跳过
      const info = this.list[this.index]
      const lsAds = uni.$getCache("ad-popup-op") || []
      if (lsAds?.length) {
        if (info?.adId) {
          const findIndex = lsAds.findIndex((item) => item.adId === info.adId)
          if (findIndex >= 0) {
            lsAds.splice(findIndex, 1, { adId: info.adId, showRule: info.showRule, opType: type, launchId: this.launchId })
          } else {
            lsAds.push({ adId: info.adId, showRule: info.showRule, opType: type, launchId: this.launchId })
          }
        } else {
          this.visible = false
        }
      } else {
        lsAds.push({ adId: info.adId, showRule: info.showRule, opType: type, launchId: this.launchId })
      }
      uni.$setCache("ad-popup-op", lsAds)
      if (info?.adId) {
        uni.$api.poster.upAdLog({ adId: info.adId, adType: info.adType, classification: info.classification || "0", operatorType: type })
      }
      this.visible = false
      if (this.index < this.list.length - 1) {
        setTimeout(() => {
          this.index++
          this.visible = true
        }, 1500)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.a {
  background-color: transparent;
}
</style>
